package com.yunhe.dao.impl;

import java.util.List;


import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yunhe.dao.ISubjectDao;

import com.yunhe.entity.Page;
import com.yunhe.entity.Subject;

/**
 * 投票内容Dao层实现
 * */
public class SubjectDaoImpl extends HibernateDaoSupport implements ISubjectDao {
	/**
	 * 条件查询集合
	 * */
	@Override
	public List<Subject> searchSubjects(Subject subject,Page page) {
		String hql = "from Subject where 1 = 1";
		if(subject.getVs_title()!=null){
			hql+=" and vs_title =:vs_title";
		}
		if(subject.getVs_type()!=null){
			hql+=" and vs_type =:vs_type";
		}
	
		//生成query结果
		Query query =this.getSession().createQuery(hql);
		query.setMaxResults(page.getEveryPage());  
          
        query.setFirstResult(page.getBeginIndex());  
		//hql语句赋值
		query.setProperties(subject);
		//返回list结果
		List<Subject> list = query.list();
		if(list.size()==0){
			return null;
		}
		return list;
		//创建list返回对象
		/*List<Subject> list = null;
		try {
			//创建session对象
			Session session = HibernateUtil.currentSession();
			//书写hql语句
			String hql = "from Subject where 1 = 1";
			if(subject.getVs_title()!=null){
				hql+=" and vs_title =:vs_title";
			}
			if(subject.getVs_type()!=null){
				hql+=" and vs_type =:vs_type";
			}
		
			//生成query结果
			Query query = session.createQuery(hql);
			query.setMaxResults(page.getEveryPage());  
	          
	        query.setFirstResult(page.getBeginIndex());  
			//hql语句赋值
			query.setProperties(subject);
			//返回list结果
			list = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		return list;*/
	}
	/**
	 * 条件查询对象
	 * */
	@Override
	public Subject searchSubject(Integer id) {
		String hql = "from Subject where vs_id = "+id;
		List<Subject> list = (List<Subject>) this.getHibernateTemplate().find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
		//创建返回对象
		/*Subject subject = null;
		try {
			//创建session对象
			Session session = HibernateUtil.currentSession();
			//书写hql语句
			String hql = "from Subject where vs_id = "+id;
			
			//生成query结果
			Query query = session.createQuery(hql);
			
			//返回结果
			subject = (Subject)query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		return subject;*/
	}
	/**
	 * 增加
	 * */
	@Override
	public void addSubject(Subject subject) {
		this.getHibernateTemplate().save(subject);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行添加方法
			session.save(subject);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}*/
		
	}
	/**
	 * 删除
	 * */
	@Override
	public void delSubject(Subject subject) {
		this.getHibernateTemplate().delete(subject);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.delete(subject);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}*/
		
	}
	/**
	 * 修改
	 * */
	@Override
	public void updateSubject(Subject subject) {
		this.getHibernateTemplate().update(subject);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.update(subject);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}*/
		
	}
	@Override
	public Integer searchSubjectCount() {
		String hql = "select count(*) from Subject where 1 = 1";
		
		//生成query结果
		Query query = this.getSession().createQuery(hql);
	
		//返回list结果
		Number num = (Number)query.uniqueResult();
		Integer count = num.intValue();
		return count;
		//创建list返回对象
		/*		Integer count = null;
				try {
					//创建session对象
					Session session = HibernateUtil.currentSession();
					//书写hql语句
					String hql = "select count(*) from Subject where 1 = 1";
				
					//生成query结果
					Query query = session.createQuery(hql);
				
					//返回list结果
					Number num = (Number)query.uniqueResult();
					count = num.intValue();
				} catch (HibernateException e) {
					e.printStackTrace();
				}finally{
					//关闭session
					HibernateUtil.closeSession();
				}
				return count;*/
	}
	@Override
	public List<Subject> searchSubjects() {
		return (List<Subject>)this.getHibernateTemplate().find("select s from Subject s");
	}
		//创建list返回对象
		/*List<Subject> list = null;
		try {
			//创建session对象
			Session session = HibernateUtil.currentSession();
			//书写hql语句
			String hql = "from Subject where 1 = 1";
			
		
			//生成query结果
			Query query = session.createQuery(hql);
		
			//返回list结果
			list = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		return list;
	}*/

}
